<template>
  <div style="width: 100%;height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
export default {
  name: "index",
  mounted() {
    this.initChart()
  },
  watch:{
    tagName:{
      handler(newValue) {
        this.tagName = newValue
        this.initChart()
      },
    },
    value:{
      handler(newValue) {
        this.value = newValue
        this.initChart()
      },
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  props:{
    title: {
      type: String,
      default: ''
    },
    value: {
      type: Array,
      default: []
    },
    tagName: {
      type: Array,
      default: []
    }
  },
  methods:{
    initChart(){
      let that = this
      that.chart = echarts.init(this.$el, 'macarons');
      that.chart.on('click',function (param) {
        that.$emit("clickPie", param.dataIndex);
      })
      let option = {
        title: {
          text: that.title,
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          data: that.value
        },
        series: [
          {
            name: '文章数量',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: that.tagName
          }
        ]
      };
      that.chart.setOption(option)
    },
  },
}
</script>

<style scoped>

</style>
